﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GOF23.Singleton
{
    /// <summary>
    /// 使用Double-check locking保证线程安全
    /// 双if检查，避免每次访问都进行加锁操作
    /// </summary>
    public sealed class Singleton3
    {
        private static Singleton3 instance = null;
        private static object obj = new object();
        private Singleton3() { }
        public static Singleton3 Instance
        {
            get 
            {
                if (instance == null)
                {
                    lock (obj)
                    {
                        if (instance == null)
                        {
                            instance = new Singleton3();
                        }
                    }
                }
                return instance; 
            }
        }
    }
}
